#https://www.hfax.com/login.html#/ 惠金所登录

import requests
import ddddocr

import base64
from PIL import Image
from io import BytesIO

import execjs


cookies = {
    'acw_tc': '2f6a1f8617059721756667215e7e431a0ffa8f8ea799bea2929c6044ee02b3',
    'forever': '1',
    'sajssdk_2015_cross_new_user': '1',
    'sensorsdata2015jssdkcross': '%7B%22distinct_id%22%3A%2218d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284%22%2C%22%24device_id%22%3A%2218d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D',
    'SESSION': 'f8b251f2-9ccc-4fc7-83bd-329d932d1eae',
}

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    # 'Cookie': 'acw_tc=2f6a1f8617059721756667215e7e431a0ffa8f8ea799bea2929c6044ee02b3; forever=1; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2218d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284%22%2C%22%24device_id%22%3A%2218d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; SESSION=f8b251f2-9ccc-4fc7-83bd-329d932d1eae',
    'Pragma': 'no-cache',
    'Referer': 'https://www.hfax.com/login.html',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'apiVersion': '1.9',
    'ds': 'home',
    'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}
# 解析图形验证码
def get_verification_code(base64_data):
    # 解码Base64数据
    image_data = base64.b64decode(base64_data.split(',')[-1])

    # 创建PIL Image对象
    image = Image.open(BytesIO(image_data))

    # 将图像保存在本地
    image.save("downloaded_image.png")

    ocr = ddddocr.DdddOcr(show_ad=False)

    res = ocr.classification(open('downloaded_image.png', 'rb').read())

    return res

# 获取图片验证码
response = requests.get('https://www.hfax.com/pc-api/common/imageCode/login', cookies=cookies, headers=headers)


cookies_login = {
    'forever': '1',
    'sajssdk_2015_cross_new_user': '1',
    'sensorsdata2015jssdkcross': '%7B%22distinct_id%22%3A%2218d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284%22%2C%22%24device_id%22%3A%2218d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D',
    'SESSION': 'f8b251f2-9ccc-4fc7-83bd-329d932d1eae',
}

headers_login = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json;charset=UTF-8',
    # 'Cookie': 'forever=1; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2218d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284%22%2C%22%24device_id%22%3A%2218d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; SESSION=f8b251f2-9ccc-4fc7-83bd-329d932d1eae',
    'Origin': 'https://www.hfax.com',
    'Pragma': 'no-cache',
    'Referer': 'https://www.hfax.com/login.html',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
    'apiVersion': '1.9',
    'distinctid': '18d33dd950712b-0c0aad4368e3fc-26001951-2073600-18d33dd95081284',
    'ds': 'home',
    'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

# 获取加密的密码
encrypt_passowrd = execjs.compile(open("逆向js.js", 'r', encoding='utf-8').read()).call('encryptByDES',"123456")

json_data_login = {
    'username': '18692739417',
    'password': encrypt_passowrd,
    'imgCode': get_verification_code(response.json()['data']['base64Str']),
    'imgToken': response.json()['data']['token'],
}

response = requests.post('https://www.hfax.com/pc-api/user/login', cookies=cookies_login, headers=headers_login, json=json_data_login)

print(response.json())


